import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mtf/app/data/UsdRecordEntity.dart';
import 'package:mtf/app/modules/stock_market_module/stock_market_controller.dart';
import 'package:mtf/app/theme/app_colors.dart';
import 'package:mtf/app/widget/AppBarLeftTitle.dart';
import 'package:mtf/app/widget/HcIconText.dart';
import 'package:mtf/app/widget/SettingBar.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../utils/AppUtils.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class stockMarketPage extends GetView<stockMarketController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarLeftTitle(
        title: "伦敦金现\nAUUSDO",
        backgroundColor: Colors.red,
      ),
      body: controller.obx((state) =>
          RefreshConfiguration(
              headerBuilder: () => MaterialClassicHeader(),
              footerBuilder: () => ClassicFooter(),
              child: SmartRefresher(
                controller: controller.refreshController,
                enablePullDown: true,
                enablePullUp: false,
                onRefresh: () {
                  controller.getNetData();
                },
                child: ListView.builder(
                  itemBuilder: (context, index) {
                    if (index == 0) {
                      return Container(
                        color: Colors.red,
                        child: Container(
                          margin: EdgeInsets.only(top: 12),
                          padding: EdgeInsets.only(
                              top: 20, left: 15, right: 15, bottom: 20),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(16),
                                  topRight: Radius.circular(16))),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    controller
                                        .lastData()
                                        .ask
                                        .toString(),
                                    style: TextStyle(
                                        fontSize: 28,
                                        fontWeight: FontWeight.bold,
                                        color: controller.curColor()),
                                  ),
                                  Row(
                                    children: [
                                      Text(
                                        controller.rateDiffPrice.value,
                                        style: TextStyle(
                                            fontSize: 18,
                                            color: controller.judgeColor(
                                                controller.rateDiffPrice
                                                    .value)),
                                      ),
                                      SizedBox(
                                        width: 10,
                                      ),
                                      Text(
                                        "${controller.rateDay.value}%",
                                        style: TextStyle(
                                            fontSize: 18,
                                            color: controller.judgeColor(
                                                controller.rateDay.value)),
                                      )
                                    ],
                                  )
                                ],
                              ),
                              SizedBox(
                                width: 15,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text.rich(TextSpan(children: [
                                    TextSpan(
                                        text: "高",
                                        style: TextStyle(
                                            color: c_333333, fontSize: 13)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text:
                                        controller
                                            .lastData()
                                            .high
                                            .toString() ??
                                            "",
                                        style: TextStyle(
                                            color: controller.curColor(),
                                            fontWeight: FontWeight.bold,
                                            fontSize: 14)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text: "昨收",
                                        style: TextStyle(
                                            color: c_333333, fontSize: 13)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text: controller
                                            .lastData()
                                            .close
                                            .toString() ??
                                            "",
                                        style: TextStyle(
                                            color: controller.curColor(),
                                            fontWeight: FontWeight.bold,
                                            fontSize: 14))
                                  ])),
                                  Text.rich(TextSpan(children: [
                                    TextSpan(
                                        text: "低",
                                        style: TextStyle(
                                            color: c_333333, fontSize: 13)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text:
                                        controller
                                            .lastData()
                                            .low
                                            .toString() ??
                                            "",
                                        style: TextStyle(
                                            color: controller.curColor(),
                                            fontWeight: FontWeight.bold,
                                            fontSize: 14)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text: "买价",
                                        style: TextStyle(
                                            color: c_333333, fontSize: 13)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text:
                                        controller
                                            .lastData()
                                            .bid
                                            .toString() ??
                                            "",
                                        style: TextStyle(
                                            color: controller.curColor(),
                                            fontWeight: FontWeight.bold,
                                            fontSize: 14))
                                  ])),
                                  Text.rich(TextSpan(children: [
                                    TextSpan(
                                        text: "开",
                                        style: TextStyle(
                                            color: c_333333, fontSize: 13)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text:
                                        controller
                                            .lastData()
                                            .open
                                            .toString() ??
                                            "",
                                        style: TextStyle(
                                            color: controller.curColor(),
                                            fontWeight: FontWeight.bold,
                                            fontSize: 14)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text: "卖价",
                                        style: TextStyle(
                                            color: c_333333, fontSize: 13)),
                                    WidgetSpan(
                                        child: SizedBox(
                                          width: 10,
                                        )),
                                    TextSpan(
                                        text:
                                        controller
                                            .lastData()
                                            .ask
                                            .toString() ??
                                            "",
                                        style: TextStyle(
                                            color: controller.curColor(),
                                            fontWeight: FontWeight.bold,
                                            fontSize: 14))
                                  ]))
                                ],
                              )
                            ],
                          ),
                        ),
                      );
                    } else if (index == 1) {
                      return Obx(() {
                        return Container(
                          padding: EdgeInsets.symmetric(
                              horizontal: 15, vertical: 10),
                          color: Colors.white,
                          child: Row(
                            children: [
                              Expanded(
                                flex: 1,
                                child: InkWell(
                                  onTap: () {
                                    controller.selectTab(0);
                                  },
                                  child: Container(
                                      padding: EdgeInsets.symmetric(
                                          vertical: 5),
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                          border: Border.all(
                                              color: c_df9e20, width: 1),
                                          gradient: controller.selectTabObs
                                              .value == 0
                                              ? LinearGradient(
                                              begin: Alignment.topLeft,
                                              //渐变开始于上面的中间开始
                                              end: Alignment.topRight,
                                              colors: [c_f4b28a, c_df9e20])
                                              : null,
                                          borderRadius:
                                          BorderRadius.all(
                                              Radius.circular(12))),
                                      child: Text(
                                        "卖出价格",
                                        style: TextStyle(
                                            color: controller.selectTabObs
                                                .value == 0
                                                ? Colors.white
                                                : c_df9e20,
                                            fontWeight: FontWeight.bold),
                                      )),
                                ),
                              ),
                              SizedBox(width: 15,),
                              Expanded(
                                flex: 1,
                                child: InkWell(
                                  onTap: () {
                                    controller.selectTab(01);
                                  },
                                  child: Container(
                                      padding: EdgeInsets.symmetric(
                                          vertical: 5),
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                          border: Border.all(
                                              color: c_df9e20, width: 1),
                                          gradient: controller.selectTabObs
                                              .value == 1
                                              ? LinearGradient(
                                              begin: Alignment.topLeft,
                                              //渐变开始于上面的中间开始
                                              end: Alignment.topRight,
                                              colors: [c_f4b28a, c_df9e20])
                                              : null,
                                          borderRadius:
                                          BorderRadius.all(
                                              Radius.circular(12))),
                                      child: Text(
                                        "买入价格",
                                        style: TextStyle(
                                            color: controller.selectTabObs
                                                .value == 1
                                                ? Colors.white
                                                : c_df9e20,
                                            fontWeight: FontWeight.bold),
                                      )),
                                ),
                              ),
                            ],
                          ),
                        );
                      });
                    } else if (index == 2) {
                      return lineCharView();
                    } else if (index == 3) {
                      return SettingBar(
                        onTap: () {
                          controller.clickTransactionRecord();
                        },
                        backgroupColor: Colors.transparent,
                        leftText: "交易记录",
                        leftTextBold: true,
                        leftTextSize: 20,
                        rightText: "查看",
                        rightTextColor: c_999999,
                        leftTextColor: c_333333,
                        rightIconSpace: 0,
                        rightIcon: Icon(
                          Icons.chevron_right_rounded,
                          color: c_999999,
                          size: 18,
                        ),
                      );
                    } else {
                      return ListView.builder(
                        itemBuilder: (context, index) {
                          return itemChange(controller.usdList[index]);
                        },
                        padding: EdgeInsets.zero,
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: controller.usdList.length,
                      );
                    }
                  },
                  shrinkWrap: true,
                  itemCount: 5,
                ),
              ))),
    );
  }

  Widget lineCharView() {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: 5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            color: Colors.white,
            width: double.infinity,
            padding: EdgeInsets.symmetric(vertical: 15, horizontal: 8),
            child: ListView.builder(
              itemBuilder: (context, index) {
                if (index == 0) {
                  return Container(
                      width: double.infinity,
                      height: 300,
                      child:  lineChart());
                } else if (index == 1) {
                  return Container(
                    width: double.infinity,
                    padding: EdgeInsets.only(left: 35, right: 0, top: 10),
                    child: Obx(() {
                      return Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(controller.startTimeObs.value),
                            Text(controller.endTimeObs.value),
                          ]);
                    }),
                  );
                } else if (index == 2) {
                  return Obx(() {
                    return Container(
                      width: double.infinity,
                      padding: EdgeInsets.symmetric(vertical: 5),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Container(
                            decoration: BoxDecoration(
                                color: controller.selectTypeObs.value == 0
                                    ? Colors.black12
                                    : null,
                                borderRadius: const BorderRadius.all(
                                    Radius.circular(16))),
                            padding: const EdgeInsets.symmetric(
                                vertical: 3, horizontal: 15),
                            child: InkWell(
                                onTap: () {
                                  controller.selectTypeObs.value = 0;
                                  controller.getNetData(emptyDialog: true);
                                },
                                child: Text("实时",
                                    style: TextStyle(
                                        color:
                                        controller.selectTypeObs.value == 0
                                            ? c_333333
                                            : c_bbbbbb))),
                          ),
                          Container(
                              decoration: BoxDecoration(
                                  color: controller.selectTypeObs.value == 1
                                      ? Colors.black12
                                      : null,
                                  borderRadius: const BorderRadius.all(
                                      Radius.circular(16))),
                              padding: const EdgeInsets.symmetric(
                                  vertical: 3, horizontal: 15),
                              child: InkWell(
                                  onTap: () {
                                    controller.selectTypeObs.value = 1;
                                    controller.getNetData(emptyDialog: true);
                                  },
                                  child: Text("近3小时",
                                      style: TextStyle(
                                          color:
                                          controller.selectTypeObs.value ==
                                              1
                                              ? c_333333
                                              : c_bbbbbb)))),
                          Container(
                              decoration: BoxDecoration(
                                  color: controller.selectTypeObs.value == 2
                                      ? Colors.black12
                                      : null,
                                  borderRadius: const BorderRadius.all(
                                      Radius.circular(16))),
                              padding: const EdgeInsets.symmetric(
                                  vertical: 3, horizontal: 15),
                              child: InkWell(
                                  onTap: () {
                                    controller.selectTypeObs.value = 2;
                                    controller.getNetData(emptyDialog: true);
                                  },
                                  child: Text("近1天",
                                      style: TextStyle(
                                          color:
                                          controller.selectTypeObs.value ==
                                              2
                                              ? c_333333
                                              : c_bbbbbb)))),
                          Container(
                              decoration: BoxDecoration(
                                  color: controller.selectTypeObs.value == 3
                                      ? Colors.black12
                                      : null,
                                  borderRadius: const BorderRadius.all(
                                      Radius.circular(16))),
                              padding: const EdgeInsets.symmetric(
                                  vertical: 3, horizontal: 15),
                              child: InkWell(
                                  onTap: () {
                                    controller.selectTypeObs.value = 3;
                                    controller.getNetData(emptyDialog: true);
                                  },
                                  child: Text("近3天",
                                      style: TextStyle(
                                          color:
                                          controller.selectTypeObs.value ==
                                              3
                                              ? c_333333
                                              : c_bbbbbb)))),
                          Container(
                              decoration: BoxDecoration(
                                  color: controller.selectTypeObs.value == 4
                                      ? Colors.black12
                                      : null,
                                  borderRadius: const BorderRadius.all(
                                      Radius.circular(16))),
                              padding: const EdgeInsets.symmetric(
                                  vertical: 3, horizontal: 15),
                              child: InkWell(
                                  onTap: () {
                                    controller.selectTypeObs.value = 4;
                                    controller.getNetData(emptyDialog: true);
                                  },
                                  child: Text("近7天",
                                      style: TextStyle(
                                          color:
                                          controller.selectTypeObs.value ==
                                              4
                                              ? c_333333
                                              : c_bbbbbb)))),
                        ],
                      ),
                    );
                  });
                }
              },
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: 3,
            ),
          )
        ],
      ),
    );
  }

  Widget lineChart() {
    return LineChart(
      LineChartData(
        backgroundColor: Colors.transparent,
        gridData: FlGridData(
            show: true,
            drawVerticalLine: false,
            getDrawingHorizontalLine: (value) {
              return const FlLine(
                color: Colors.black12,
                strokeWidth: 0.5,
              );
            }),
        borderData: FlBorderData(show: false),
        lineBarsData: controller.getChartBars(),
        titlesData: controller.getChartTitle(),
        lineTouchData: LineTouchData(handleBuiltInTouches: true,touchTooltipData: LineTouchTooltipData(
          tooltipBgColor: c_df9e20,
          getTooltipItems: (List<LineBarSpot> lineBarsSpot) {
            return lineBarsSpot.map((lineBarSpot) {
              return LineTooltipItem(
                "${lineBarSpot.y.toString()}\n${controller.getTimeValue(lineBarSpot.x.toInt())}",
                const TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                ),
              );
            }).toList();
          }
        )),
      ),
      duration: Duration(milliseconds: 1000), // Optional
      curve: Curves.linear, // Optional
    );
  }

  Widget itemChange(ListUSDElement bean) {
    return Container(
      margin: EdgeInsets.only(bottom: 10, left: 10, right: 10),
      padding: EdgeInsets.only(left: 10, right: 10, bottom: 15),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(10)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SettingBar(
              leftText: bean.actionTxt ?? "",
              paddingHorizontal: 0,
              leftIconSpace: 0,
              rightIconSpace: 0,
              rightText: "订单号：${bean.order ?? ""}",
              rightTextColor: c_666666,
              rightTextSize: 12,
              backgroupColor: Colors.transparent,
              showDivider: true),
          SizedBox(
            height: 15,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "交易品种",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              Text(
                "交易量(手数)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              Text(
                "交易价格(USD)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                bean.symbol ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
              Text(
                bean.volumeTxt.toString() ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
              Text(
                bean.price ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "手续费(USD)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              Text(
                "隔夜利息(USD)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              Text(
                "盈亏(USD)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                bean.priceTp ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
              Text(
                bean.storage ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
              Text(
                bean.profit ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "时间",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              SizedBox(
                height: 5,
              ),
              Text(
                  AppUtils.dateToString(bean.timeAt),
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
            ],
          )
        ],
      ),
    );
  }
}
